package com.systemsjr.mpf.db;

import java.util.HashMap;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import com.systemsjr.mpf.account.Account;

public class AccountContentProvider extends MpfContentProvider {
	public static final String AUTHORITY = "com.systemsjr.mpf.db.AccountContentProvider";
	private static final int ACCOUNTS = 1;
	// URI MATCH OF A SPECIFIC CITIZEN QUERY
	private static final int ACCODE = 2;

	//private static final int ACCOUNTID = 3;

	protected static UriMatcher sUriMatcher;
	protected static HashMap<String, String> projectionMap;

	/*
	 * public static Cursor getAllAccounts(Context context) { MpfConnector
	 * connector = new MpfConnector(context);
	 * 
	 * return connector.get(Account.TABLE_NAME, new String[] { Account.ID,
	 * Account.CODE, Account.ACCOUNT }, ""); }
	 * 
	 * public static List<Account> getAllAccountList(Context context){
	 * 
	 * List<Account> list = new ArrayList<Account>(); Cursor c =
	 * getAllAccounts(context);
	 * 
	 * while(c.moveToNext()){ list.add(new Account(c.getLong(0), c.getString(1),
	 * c.getString(2))); }
	 * 
	 * return list; }
	 * 
	 * public static Account getById(Context context, long id) { MpfConnector
	 * connector = new MpfConnector(context);
	 * 
	 * Cursor c = connector.get(Account.TABLE_NAME, new String[] { Account.ID,
	 * Account.CODE, Account.ACCOUNT }, "ID = " + id);
	 * 
	 * Account a = null; if(c.moveToFirst()){ a = new Account(c.getLong(0),
	 * c.getString(1), c.getString(2)); } return a;
	 * 
	 * }
	 * 
	 * public static Account getByCode(Context context, String code) {
	 * MpfConnector connector = new MpfConnector(context);
	 * 
	 * Cursor c = connector.get(Account.TABLE_NAME, new String[] { Account.ID,
	 * Account.CODE, Account.ACCOUNT }, "CODE = " + code);
	 * 
	 * Account a = new Account(c.getLong(0), c.getString(1), c.getString(2));
	 * 
	 * return a;
	 * 
	 * }
	 * 
	 * public static void saveAccount(Context context, Intent intent, long
	 * rowId, ContentValues contentValues) { MpfConnector connector = new
	 * MpfConnector(context);
	 * 
	 * if (intent.getExtras() == null) { connector.insert(Account.TABLE_NAME,
	 * null, contentValues); } else { connector.update(Account.TABLE_NAME,
	 * contentValues, rowId); } }
	 */

	@Override
	public boolean onCreate() {
		dbHelper = new MpfDbOpenHelper(this.getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		qb.setTables(Account.TABLE_NAME);
		System.out.println(sUriMatcher.toString());
		System.out.println(uri.toString());
		switch (sUriMatcher.match(uri)) {
		case ACCOUNTS:
			qb.setProjectionMap(projectionMap);
			break;
		case ACCODE:
			String code = uri.getPathSegments().get(Account.CODE_PATH_POSITION);
			qb.setProjectionMap(projectionMap);
			qb.appendWhere(Account.CODE + " like %" + code + "%");
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor c = qb.query(db, projection, selection, selectionArgs, null,
				null, sortOrder);
		c.setNotificationUri(getContext().getContentResolver(), uri);

		return c;
	}

	@Override
	public String getType(Uri uri) {

		switch (sUriMatcher.match(uri)) {
		case ACCOUNTS:
			return Account.CONTENT_TYPE;
		case ACCODE:
			return Account.CONTENT_ITEM_TYPE;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {

		if (sUriMatcher.match(uri) != ACCOUNTS) {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		ContentValues contentValues;

		if (values != null) {
			contentValues = new ContentValues(values);
		} else {
			contentValues = new ContentValues();
		}

		SQLiteDatabase db = dbHelper.getWritableDatabase();

		long rowid = db.insert(Account.TABLE_NAME, null, contentValues);

		if (rowid > 0) {
			Uri accountUri = ContentUris.withAppendedId(Account.CONTENT_URI,
					rowid);
			getContext().getContentResolver().notifyChange(accountUri, null);
			return accountUri;
		}
		throw new SQLException("Failed to insert row into " + uri);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int count;

		switch (sUriMatcher.match(uri)) {
		case ACCOUNTS:
			count = db.delete(Account.TABLE_NAME, selection, selectionArgs);
			break;
		case ACCODE:
			String code = uri.getPathSegments().get(Account.CODE_PATH_POSITION);
			String finalWhere = Account.TABLE_NAME + " like %" + code + "%";
			if (selection != null) {
				finalWhere = finalWhere + selection;
			}

			count = db.delete(Account.TABLE_NAME, finalWhere, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int count;

		switch (sUriMatcher.match(uri)) {
		case ACCOUNTS:
			count = db.update(Account.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		case ACCODE:
			String code = uri.getPathSegments().get(Account.CODE_PATH_POSITION);
			String finalWhere = Account.TABLE_NAME + "=" + code;
			if (selection != null) {
				finalWhere = finalWhere + selection;
			}

			count = db.update(Account.TABLE_NAME, values, finalWhere,
					selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(AUTHORITY, "account", ACCOUNTS);
		sUriMatcher.addURI(AUTHORITY, "account/#", ACCODE);
		// PROJECTION MAP USED FOR ROW ALIAS
		projectionMap = new HashMap<String, String>();
		projectionMap.put(Account.ID, Account.ID);
		projectionMap.put(Account.CODE, Account.CODE);
		projectionMap.put(Account.ACCOUNT, Account.ACCOUNT);
	}

}
